/***************************************************************************
/
/ the ImageManager class manages the uploading, changing, and
/ deleting of all images (via the adminstrative controller)
/
/**************************************************************************/

//used necessary for CommonsMultipartFile (managing files)
import org.springframework.web.multipart.commons.*


class ImageManager {


	//return true if the file is a valid picure for our application
	static public isFileTypeValidPicture( CommonsMultipartFile file ) {

		if( !( file == null || file.empty ) ) {

			//return true if this filetype is a valid picture
			String fileType = file.getContentType()
			if ( AppConstant.VALID_IMAGE_TYPES.contains( fileType ) ) {

				return true

			}
		}

		//file type does not meet our requirements
		return false

	}


	//return true if the file no larger than our limit
	static public isFileSizeWithinLimit( CommonsMultipartFile file ) {

		if( !( file == null || file.empty ) ){

			//check size of file, return true if it meets size requirements
			if ( file.getSize() <= AppConstant.IMAGE_SIZE_MAX_BYTES ) {

				return true

			}
		}

		//file size does not meet our requirements
		return false

	}


	//return an original (unique) file name based on the original file
	//name and the current date/time down to the second
	static public createUniqueFileName(String originalFileName) {

		String uniqueName = ""

		//ensure an original file name was passed in
		if ( !( originalFileName == null || "".equals( originalFileName ) ) ) {

			//split original file name into name and extention (
			String fileExtension = ""
			String fileName = ""
			if ( originalFileName.indexOf( "." ) != -1 ) {

				//if the file contains a dot, then it has an extionsion - split the file
				fileName = getFileNameWithoutExtention( originalFileName )
				fileExtension = getFileExtension( originalFileName )

			} else {

				//if there is no extension just use the entire name for the file
				fileName = originalFileName

			}

			//create a new unique name based on concatinaiton of original name and date/time
			//(replacing all spaces and punctuation with an underscore)
			String dateTime = new Date()
			String regExp = "[\\p{Punct}\\s]"
			String replaceChar = "_"
			uniqueName = fileName.replaceAll( regExp, replaceChar ) +
						 replaceChar +
						 dateTime.replaceAll( regExp, replaceChar ) +
						 fileExtension

		}

		//finally we can return a unique file name
		return uniqueName

	}


	//return just the file name of a file without the extension
	static private getFileNameWithoutExtention(String originalFileName) {
		return originalFileName.substring(0, originalFileName.lastIndexOf( "." ) )
	}


	//return just the extension of a file
	static private getFileExtension(String originalFileName) {
		return originalFileName.substring(originalFileName.lastIndexOf( "." ) )
	}


	//update the default picture name for a story
	static public updateDefaultPictureForStory(String pictureName, String storyId) {

		def story = Story.get( storyId )
		story.defaultPictureName = pictureName
		story.save()

	}


	//add a new 'additional image' to a story
	static public addAdditionalImageForStory( String pictureName, String storyId ) {

		def story = Story.get( storyId )
		def storyImage = new StoryImage()
		storyImage.pictureName = pictureName
		storyImage.story = story
		storyImage.save()

	}

}
